package ferp.android.tasks.center;

import android.os.AsyncTask;
import ferp.android.social.Connector;
import ferp.center.client.Action;
import ferp.core.log.Log;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * User: igorgok
 * Date: 6/9/13 3:30 PM
 */
public class TaskPublishReport extends AsyncTask<Void, Void, Void>
{
  public TaskPublishReport(List<Connector> success, Map<Connector, Connector.PostError> failure)
  {
    this.success = success;
    this.failure = failure;
  }

  @Override
  protected Void doInBackground(Void... objects)
  {
    success();
    failure();

    return null;
  }

  private void success()
  {
    for (Connector c : success)
      try
      {
        Action.execute("social/publish/success/" + c.getClass().getSimpleName().substring(0, 2), null, null);
      }
      catch (IOException e)
      {
        Log.error(Log.TAG, e);
      }
  }

  private void failure()
  {
    for (Map.Entry<Connector, Connector.PostError> me : failure.entrySet())
    {
      Connector c = me.getKey();
      Connector.PostError error = me.getValue();

      try
      {
        Action.execute("social/publish/failure/" + c.getClass().getSimpleName().substring(0, 2), error, null);
      }
      catch (IOException e)
      {
        Log.error(Log.TAG, e);
      }
    }
  }

  private final List<Connector> success;
  private final Map<Connector, Connector.PostError> failure;
}
